package org.mineground.commands.player.admin;

import java.util.List;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.mineground.Main;
import org.mineground.PermissionNodes;

/**
 * @file Locate.java (01.03.2012)
 * @author Daniel Koenen
 *
 */
public class Locate implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] arguments) {
        Player player = (Player) cs;
        
        if (!player.hasPermission(PermissionNodes.PERMISSION_LOCATE_COMMAND)) {
            return true;
        }
        
        if (arguments.length < 1) {
            player.sendMessage(ChatColor.RED + "* Usage: '/locate <Playername>'");
            return true;
        }
        
        Player locatePlayer = Main.getInstance().getServer().getPlayer(arguments[0]);
        
        if (locatePlayer == null) {
            player.sendMessage(ChatColor.RED + "* Error: Invalid player.");
            return true;
        }
        
        if (!locatePlayer.isOnline()) {
        	player.sendMessage(ChatColor.AQUA + "* The player you're trying to locate is not online.");
        	return true;
        }
        
        if (locatePlayer.isDead()) {
        	player.sendMessage(ChatColor.DARK_AQUA + "* The player you're locating is dead. May god have his/her soul.");
        }
        
        if(locatePlayer.isSneaking()) {
        	player.sendMessage(ChatColor.DARK_AQUA + "* This player is trying to play like 007.");
        }
        Location location = locatePlayer.getLocation();
        StringBuilder locateString = new StringBuilder();
        locateString.append(ChatColor.AQUA);
        locateString.append(locatePlayer.getName());
        locateString.append(ChatColor.GOLD);
        locateString.append(" is currently located at ");
        locateString.append(ChatColor.AQUA);
        locateString.append(location.getBlockX());
        locateString.append(ChatColor.GOLD);
        locateString.append(", ");
        locateString.append(ChatColor.AQUA);
        locateString.append(location.getBlockY());
        locateString.append(ChatColor.GOLD);
        locateString.append(", ");
        locateString.append(ChatColor.AQUA);
        locateString.append(location.getBlockZ());
        locateString.append(ChatColor.GOLD);
        locateString.append(" in world ");
        locateString.append(ChatColor.AQUA);
        locateString.append(location.getWorld().getName());
        locateString.append(ChatColor.GOLD);
        locateString.append(". He/she is currently holding ");
        locateString.append(ChatColor.AQUA);
        locateString.append(locatePlayer.getItemInHand().getType().toString());
        locateString.append(ChatColor.GOLD);
        locateString.append(" in his/her hand.");
        List<Entity> nearbyEntities = locatePlayer.getNearbyEntities(10, 10, 10);
        if (nearbyEntities != null && nearbyEntities.size() > 0) {
        	locateString.append(ChatColor.DARK_AQUA);
            locateString.append("He is also surrounded by monsters so watch out!");
        }
        player.sendMessage(locateString.toString());
        return true;
    }
}
